package com.fyzk.activity.zoushitu;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.fyzk.bean.ChartItem;
import com.tyjc.lottery.R;
import com.fyzk.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;


public class MyLL extends LinearLayout{
	
	private Context context;
	private boolean isLan = false;
	
	private int pmW,pmH,pmD1;
	private float pmD2;
	
	private int w = 40;
	private int h = 41;
	
	private int color_zi_b;
	private int color_zi_c;
	private int zi_cxcs;
	private int zi_pjlc;
	private int zi_zdyl;
	private int zi_zdlc;
	private int bg_b;
	private int bg_h;
	private int line_shu;
	private int color_zhexian;
	
	private Bitmap qiu_btm;
	
	
	private int[][] kj = new int[][]{};
	public ArrayList<ChartItem> arrayChartItems = null;
	private String[][] miss;
	private int widthNum = 16;
	private boolean isZX = true,isYL = true,isTJ = true;
	
	private final int count = 33;
//	private int qiShu;
	/**
	 * 出现次数
	 */
	private int [] cxcs = new int[100];
	/**
	 * 平均遗漏
	 */
	private int [] pjyl = new int[100];
	/**
	 * 最大遗漏
	 */
	private int [] zdyl = new int[100];
	/**
	 * 最大连出
	 */
	private int [] zdlc = new int[100];
	
	
	public MyLL(Context context){
		this(context, null);
	}

	public MyLL(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		setFocusable(true);
		
		this.pmW = AppUtil.getWidth();
		this.pmH = AppUtil.getHeight();
		this.pmD1 = AppUtil.getDensityDpi();
		this.pmD2 =  AppUtil.getDensity();
		
		w = pmW/14;
		h = w+1;
		
		this.context = context;
//		this.qiShu = ZST_infor.kj2.length;
		
		color_zi_b = context.getResources().getColor(R.color.white);
		color_zi_c = context.getResources().getColor(R.color.zi_c);
		bg_b = context.getResources().getColor(R.color.bgb);
		bg_h = context.getResources().getColor(R.color.bgh_c);
//		bg_qiu = context.getResources().getDrawable(R.drawable.lanqiu_check);
		line_shu = context.getResources().getColor(R.color.shu_line);
		zi_cxcs = context.getResources().getColor(R.color.cxcs);
		zi_pjlc = context.getResources().getColor(R.color.pjlc);
		zi_zdlc = context.getResources().getColor(R.color.zdlc);
		zi_zdyl = context.getResources().getColor(R.color.zdyl);
		color_zhexian = context.getResources().getColor(R.color.zhexian_lan);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		Rect rect;
		List<Rect> rects = new ArrayList<Rect>();
		List<Integer> hao = new ArrayList<Integer>();
		Paint paint_bg = new Paint(Paint.ANTI_ALIAS_FLAG);
		Paint paint_zi = new Paint(Paint.ANTI_ALIAS_FLAG);
		Paint paint_line = new Paint(Paint.ANTI_ALIAS_FLAG);
		Paint paint_zheXian = new Paint(Paint.ANTI_ALIAS_FLAG);
		
		if (miss == null) {
			return;
		}
		
		String text = "-";
		for (int i = 0; i < (miss.length+4); i++) {
			int [] lianchu = new int[count];
			
			paint_zi.setColor(color_zi_c);
			paint_zi.setTextSize(14*pmD2);
			paint_zi.setTextAlign(Align.CENTER);
			paint_line.setColor(line_shu);
			paint_zheXian.setColor(color_zhexian);
			if (i%2 == 0) {
				paint_bg.setColor(bg_h);
			}else {
				paint_bg.setColor(bg_b);
			}
			for (int j = 0; j < widthNum; j++) {
				rect = new Rect(j*w, i*h, (j+1)*w-2, (i+1)*h);
				canvas.drawRect(rect, paint_bg);
				if (i==miss.length) {
					paint_zi.setColor(zi_cxcs);
					
					if (isTJ) {
						text = cxcs[j]+"";
					}
					
					drawText(canvas, paint_zi, ""+text, rect);
				}else if (i==(miss.length+1)) {
					paint_zi.setColor(zi_pjlc);
					
					if (isTJ) {
						text = pjyl[j]+"";
					}
					
					drawText(canvas, paint_zi, ""+text, rect);
				}else if (i==(miss.length+2)) {
					paint_zi.setColor(zi_zdyl);
					
					if (isTJ) {
						text = zdyl[j]+"";
					}
					
					drawText(canvas, paint_zi, ""+text, rect);
				}else if (i==(miss.length+3)) {
					paint_zi.setColor(zi_zdlc);

					if (isTJ) {
						text = zdlc[j]+"";
					}
					
					drawText(canvas, paint_zi, ""+text, rect);
				}else {
					if (miss[i][j].equals("0")) {
						rects.add(rect);
						hao.add(j+1);
					}else{
						if (isYL) {
							
							drawText(canvas, paint_zi, ""+miss[i][j], rect);
						}
					}
				}
				canvas.drawRect(j*w-2, i*h, j*w, (i+1)*h, paint_line);
			}
			
		}
		
		//画球
		for (int k = 0; k < rects.size(); k++) {
			if (k < (rects.size()-1) && isLan && isZX) {
				
				paint_zheXian.setStrokeWidth((float)2.5);//1.0为正常粗细
				canvas.drawLine(rects.get(k).centerX(), rects.get(k).centerY(), rects.get(k+1).centerX(), rects.get(k+1).centerY(), paint_zheXian);
			}
			
			canvas.drawBitmap(qiu_btm, null, rects.get(k), paint_bg);
		
			paint_zi.setColor(color_zi_b);
			if (hao.get(k) < 10) {
				
				drawText(canvas, paint_zi, "0"+hao.get(k), rects.get(k));
			}else {
				drawText(canvas, paint_zi, ""+hao.get(k), rects.get(k));
			}
		}
		
		
		
	}
	
	/**
	 * 绘制文字
	 * @param canvas
	 * @param paint
	 * @param text
	 * @param rect
	 */
	private void drawText(Canvas canvas,Paint paint,String text,Rect rect){
		FontMetrics fm = paint.getFontMetrics();
		int baseLine = (int)(rect.top + (rect.bottom - rect.top - fm.bottom + fm.top) / 2 - fm.top);  
		canvas.drawText(text, rect.centerX(), baseLine, paint);
	}

	public void setQiu_btm(int id) {
		this.qiu_btm = BitmapFactory.decodeResource(context.getResources(), id);
	}

	public void setKj(int[][] kj) {
		this.kj = kj;
	}

	public void setLan(boolean isLan) {
		this.isLan = isLan;
	}

	public ArrayList<ChartItem> getArrayChartItems() {
		return arrayChartItems;
	}

	public void setArrayChartItems(ArrayList<ChartItem> arrayChartItems) {
		this.arrayChartItems = arrayChartItems;
		miss = new String[arrayChartItems.size()][];
		for (int i = 0; i < arrayChartItems.size(); i++) {
			if (isLan) {
				miss[i] = arrayChartItems.get(i).lanMiss();
			}else{
				miss[i] = arrayChartItems.get(i).hongMiss();
			}
			widthNum = miss[i].length;
			
		}
		
		clear();
		
		ArrayList<ArrayList<Integer>> yls = new ArrayList<ArrayList<Integer>>();
		for (int i = 0; i < miss[0].length; i++) {
			int lc = 1;
			zdlc[i] = lc;
			ArrayList<Integer> yl = new ArrayList<Integer> (); 
			for (int j = 0; j < miss.length; j++) {
				if (miss[j][i].equals("0")) {
					cxcs[i]++;
				}
				
				if (Integer.parseInt(miss[j][i]) > zdyl[i]) {
					zdyl[i] = Integer.parseInt(miss[j][i]);
				}
				
				if (j > 0 && miss[j-1][i].equals("0") && miss[j][i].equals("0")) {
					lc++;
					if (lc > zdlc[i]) {
						zdlc[i] = lc;
					}
				}else{
					lc = 1;
				}
				
				if (j > 0 && miss[j][i].equals("0")) {
					yl.add(Integer.parseInt(miss[j-1][i]));
				}
				
				
			}
			yls.add(yl);
		}
		
		for (int i = 0; i < yls.size(); i++) {
			int yl_h = 0;
			for (int j = 0; j < yls.get(i).size(); j++) {
				yl_h += yls.get(i).get(j);
			}
			if (cxcs[i] == 0) {
				pjyl[i] = zdyl[i];
			}else{
				
				pjyl[i] = yl_h / cxcs[i];
			}
		}
	}
	
	public void setBoolean(boolean isYL,boolean isZX,boolean isTJ){
		this.isYL = isYL;
		this.isZX = isZX;
		this.isTJ = isTJ;
	}
	
	private void clear(){
		for (int i = 0; i < zdlc.length; i++) {
			cxcs[i] = 0;
			pjyl[i] = 0;
			zdyl[i] = 0;
			zdlc[i] = 0;
		}
	}
	
	
}
